//
//  CalendarPickerView.swift
//  Hyphenate-Demo-Swift
//
//  Created by cydida on 2019/4/8.
//  Copyright © 2019年 curtainups. All rights reserved.
//

import UIKit

/// 宽度比
let kScalWidth = (kScreenWidth / 375)
/// 高度比
let kScalHeight = (kScreenHeight / 667)

/// RGB颜色
func kRGBColor(_ r:CGFloat,_ g : CGFloat, _ b : CGFloat, _ p : CGFloat) -> UIColor {
    
    return UIColor.init(red: r / 255.0, green: g / 255.0, blue: b / 255.0, alpha: p)
}


protocol PickerDelegate {
    
    func selectedPicker(_ pickerView : CalendarPickerView,_ title : String,_ hour : String,_ type : String)
}

class CalendarPickerView: UIView,UIPickerViewDelegate, UIPickerViewDataSource{

    
    var pickerDelegate : PickerDelegate?
    private let pickerH : CGFloat! = 260 * kScalHeight
    private var addressPicker : UIPickerView = UIPickerView()
    private var backgroundButton : UIButton = UIButton()
    private var titleArray = ["meeting".localized(),"work".localized(),"study".localized(),"social".localized(),"meal".localized(),"entertainment".localized()]
    private var hourArray = ["0.5 hour".localized(),"1 hour".localized(),"1.5 hour".localized(),"2 hours".localized(),"2.5 hours".localized(),"3 hours".localized()]
    private var typeArray = ["call allowed".localized(),"text allowed".localized(),"do not disturb".localized()]
    private var selectedTitle = ""
    private var selectedHour = ""
    private var selectedType = ""
    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        
        var title = UILabel()
        title.font = UIFont.systemFont(ofSize: 17, weight: UIFont.Weight.light)
        title.textAlignment = .center
        
        if component == 0{
            title.text =  titleArray[row]
        }else if component == 1{
            title.text =  hourArray[row]
        }else{
            title.text =  typeArray[row]
        }
        return title
        
    }
    // MARK: - 初始化UI
    init(_ delegate : PickerDelegate){
        
        
        selectedTitle = self.titleArray[0]
        selectedHour = self.hourArray[0]
        selectedType = self.typeArray[0]
        
        pickerDelegate = delegate
        let frame = CGRect.init(x: 0, y: kScreenHeight, width: kScreenWidth, height: kScreenHeight)
        super.init(frame: frame)
        let view = UIView.init(frame: CGRect.init(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight))
        view.backgroundColor = kRGBColor(230, 230, 230, 1)
        self.addSubview(view)
        
        // 取消按钮
        let cancelButton = UIButton.init(type: UIButtonType.custom)
        cancelButton.frame = CGRect.init(x: 0, y: 0, width: 60, height: 44)
        cancelButton.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        cancelButton.setTitle(" ", for: UIControlState.normal)
        cancelButton.setTitleColor(kRGBColor(18, 93, 255, 1), for: UIControlState.normal)
        cancelButton.addTarget(self, action: #selector(cancelButtonClick), for: UIControlEvents.touchUpInside)
        self.addSubview(cancelButton)
        
        // 确定按钮
        let doneButton = UIButton.init(type: UIButtonType.custom)
        doneButton.frame = CGRect.init(x: kScreenWidth - 60, y: 0, width: 60, height: 44)
        doneButton.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        doneButton.setTitle("OK".localized(), for: UIControlState.normal)
        doneButton.setTitleColor(kRGBColor(18, 93, 255, 1), for: UIControlState.normal)
        doneButton.addTarget(self, action: #selector(doneButtonClick), for: UIControlEvents.touchUpInside)
        self.addSubview(doneButton)
        
        backgroundButton = UIButton.init(type: UIButtonType.system)
        backgroundButton.frame = CGRect.init(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight)
        backgroundButton.backgroundColor = kRGBColor(0, 0, 0, 0)
        backgroundButton.addTarget(self, action: #selector(cancelButtonClick), for: UIControlEvents.touchUpInside)

        addressPicker = UIPickerView.init(frame: CGRect.init(x: 0, y: 44, width: kScreenWidth, height: pickerH - 44))
        addressPicker.delegate = self
        addressPicker.dataSource = self
        
        addressPicker.backgroundColor = UIColor.white

        self.addSubview(addressPicker)
 
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: - Method
    
    /// 取消按钮点击方法
    @objc func cancelButtonClick(){
        
        self.pickerViewHidden()
    }
    
    /// 确定按钮点击方法
    @objc func doneButtonClick(){
        
        pickerDelegate?.selectedPicker(self, selectedTitle, selectedHour, selectedType)
 
        self.pickerViewHidden()
    }
    /// 时间选择
    ///
    /// - Parameter datePicker: 时间选择器
    @objc func dateSelected(_ datePicker: UIDatePicker) {
        
        
    }
    
    /// 读取省市区数据
    func getAddressData() {
        
        self.pickerView(addressPicker, didSelectRow: 0, inComponent: 0)
    }
    /// 展示pickerView
    public func pickerViewShow() {
        
        let keyWindow = UIApplication.shared.keyWindow
        keyWindow?.addSubview(self.backgroundButton)
        keyWindow?.addSubview(self)
        UIView.animate(withDuration: 0.4, animations: {
            self.backgroundButton.backgroundColor = kRGBColor(0, 0, 0, 0.3)
            self.frame.origin.y = kScreenHeight - self.pickerH
        }) { (complete: Bool) in
            
        }
    }
    /// 隐藏pickerView
    public func pickerViewHidden() {
        
        UIView.animate(withDuration: 0.4, animations: {
            self.backgroundButton.backgroundColor = kRGBColor(0, 0, 0, 0)
            self.frame.origin.y = kScreenHeight
        }) { (complete:Bool) in
            self.removeFromSuperview()
            self.backgroundButton.removeFromSuperview()
        }
    }
    
    // MARK: - UIPickerViewDelegate, UIPickerViewDataSource
    
    /// 返回列
    ///
    /// - Parameter pickerView: pickerView
    /// - Returns: 列
    func numberOfComponents(in pickerView: UIPickerView) -> Int {

        return 3

        
    }
    
    /// 返回对应列的行数
    ///
    /// - Parameters:
    ///   - pickerView: pickerView
    ///   - component: 列
    /// - Returns: 行
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        
        if component == 0{
            return titleArray.count
        }else if component == 1{
            return hourArray.count
        }else{
            return typeArray.count
        }

    }
    
    /// 返回对应行的title
    ///
    /// - Parameters:
    ///   - pickerView: pickerView
    ///   - row: 行
    ///   - component: 列
    /// - Returns: title
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        var title = ""

            if component == 0
            {
                title = titleArray[row]
                return title
            }
            else if component == 1
            {
                title = hourArray[row]
                return title
            }
            else
            {
                title = typeArray[row]
                return title
            }

    }
    
    /// 选择列、行
    ///
    /// - Parameters:
    ///   - pickerView: pickerView
    ///   - row: 行
    ///   - component: 列
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        
        if component == 0 {
            selectedTitle = titleArray[row]
        }
        else if component == 1
        {
            selectedHour = hourArray[row]
//            self.pickerView(pickerView, didSelectRow: 0, inComponent: 2)
        }
        else
        {
            selectedType = typeArray[row]
        }
        pickerView.reloadAllComponents()
    }
}
